// pages/user/invoiceMoney/invoiceMoney.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:'',//金额
    address_len:0,
    address_icon: '../../../images/address_icon.png',
    position_r: '../../../images/position_r.png',
    go_right: '../../../images/go_right.png',
    add_red: '../../../images/add_red.png',
    address: {}, //地址信息
    address_id:'',//当前选中地址id
    invoice_id: '',  // 发票ID
    c_Coin:0,//可开金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    if (options.invoice_id != 'undefined') {
      that.setData({
        invoice_id: options.invoice_id
      })
    }else{
      app.showToast('发票抬头信息有误,请重选!')
    }
    that.getcanCoin();//获取可开金额
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var address_id = wx.getStorageSync('address_id');
    if (address_id != '') {
      that.addressDisplay(address_id); //重新获取地址信息
    } else {
      that.setData({
        address: {},
        address_len: 0,
        address_id: ''
      })
      that.getAddressInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('address_id') //同步清除地址id缓存
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  //出入金额
  changeMoney(e){
    var that=this;
    that.setData({
      money:e.detail.value
    })
  },
  //查询地址信息
  getAddressInfo: function () {
    var that = this;
    app.ajax({
      url: 'User/Useraddress/addressList',
      method: "POST",
      data: {
        page: 1,
        pageSize: 100
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          var address = res.data.data[0];
          var address_len = res.data.data.length;
          var address_id = res.data.data[0].address_id;
          that.setData({
            address: address,
            address_len: address_len,
            address_id: address_id
          })
        } else {
          // app.showToast(res.data.message);
        }
      }
    })
  },
  //单个地址信息
  addressDisplay: function (address_id) {
    var that = this;
    app.ajax({
      url: 'User/Useraddress/addressDisplay',
      method: "POST",
      data: {
        address_id: address_id
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          var addressb = res.data.data;
          var addressb_len = 1;
          var addressb_id = res.data.data.address_id;
          that.setData({
            address: addressb,
            address_len: addressb_len,
            address_id: addressb_id
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //跳转我的地址列列表
  myAddressList() {
    var that = this;
    var address_len = that.data.address_len;
    if (address_len == 0) {
      var address_id = '';
      var Type = 1;
      wx.navigateTo({
        url: '../../user/editAddress/editAddress?address_id=' + address_id + '&type=' + Type,
      })
    } else {
      wx.navigateTo({
        url: '../../user/myAddressList/myAddressList?on_way=' + 2,
      })
    }

  },
  //获取可开金额
  getcanCoin(){
      var that=this;
    // ajax请求
    app.ajax({
      url: 'User/Invoice/getInvoiceableAmount',
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            c_Coin: res.data.data.remain_money
          })
        } else {
          app.showToast(res.data.message);
        }

      }
    })
  },
  //提交数据
  upDataFun() {
    var that = this;
    var invoice_id = that.data.invoice_id;
    var mo_ney = that.data.money;
    var cCoin = that.data.c_Coin;
    var addressId = that.data.address_id;
    // app.showToast('提交审核成功!请耐心等待');
    // setTimeout(function () {
    //   wx.navigateBack({
    //     delta: 2
    //   })
    // }, 3500)
    // return false;
    if (mo_ney == '') {
      app.showToast('请输入开票金额!')
      return false;
    }
    if (mo_ney == 0) {
      app.showToast('开票金额不能为0!')
      return false;
    }
    if (mo_ney > cCoin){
      app.showToast('输入金额不能大于可开票金额!','none',4000)
      return false;
    }
    if (addressId==''){
      app.showToast('请选择收发票地址!', 'none', 4000)
      return false;
    }
    // ajax请求
    app.ajax({
      url: 'User/Invoice/applyOpen',
      data: {
        invoice_type: 1,
        money: that.data.money,
        invoice_recevice_address: addressId,
        invoice_id: invoice_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          // 数据赋值 
          app.showToast(res.data.message);
          setTimeout(function () {
            wx.navigateBack({
              delta: 2
            })
          }, 3500)
        } else {
          app.showToast(res.data.message);
        }

      }
    })

  },
})